package co.com.desarrollo.pedidos.config.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter;

import co.com.desarrollo.pedidos.services.GeneralService;
import co.com.desarrollo.pedidos.services.PedidoService;
import co.com.desarrollo.pedidos.services.ProductoService;
import co.com.desarrollo.pedidos.services.TiendaService;
import co.com.desarrollo.pedidos.services.UsuarioService;
import co.com.desarrollo.pedidos.services.impl.GeneralServiceImpl;
import co.com.desarrollo.pedidos.services.impl.PedidoServiceImpl;
import co.com.desarrollo.pedidos.services.impl.ProductoServiceImpl;
import co.com.desarrollo.pedidos.services.impl.TiendaServiceImpl;
import co.com.desarrollo.pedidos.services.impl.UsuarioServiceImpl;

@Configuration
public class ConfigurationServices {

	private @Value("#{'${rmi.portRegistry}'}") Long portRegistry;
	private @Value("#{'${rmi.portService}'}") Long portService;
	
	@Bean
	public GeneralService generalService() {
		return new GeneralServiceImpl();
	}
	
	@Bean
	public RmiServiceExporter generalRmiServiceExporter()
	{
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
		rmiServiceExporter.setServiceName("GeneralService");
		rmiServiceExporter.setServiceInterface(GeneralService.class);
		rmiServiceExporter.setService(generalService());
		rmiServiceExporter.setRegistryPort(portRegistry.intValue());
		rmiServiceExporter.setServicePort(portService.intValue());
		return rmiServiceExporter;
	}
	
	@Bean
	public ProductoService productoService() {
		return new ProductoServiceImpl();
	}
	
	@Bean
	public RmiServiceExporter productoRmiServiceExporter()
	{
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
		rmiServiceExporter.setServiceName("ProductoService");
		rmiServiceExporter.setServiceInterface(ProductoService.class);
		rmiServiceExporter.setService(productoService());
		rmiServiceExporter.setRegistryPort(portRegistry.intValue());
		rmiServiceExporter.setServicePort(portService.intValue());
		return rmiServiceExporter;
	}
	
	@Bean
	public TiendaService tiendaService() {
		return new TiendaServiceImpl();
	}
	
	@Bean
	public RmiServiceExporter tiendaRmiServiceExporter()
	{
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
		rmiServiceExporter.setServiceName("TiendaService");
		rmiServiceExporter.setServiceInterface(TiendaService.class);
		rmiServiceExporter.setService(tiendaService());
		rmiServiceExporter.setRegistryPort(portRegistry.intValue());
		rmiServiceExporter.setServicePort(portService.intValue());
		return rmiServiceExporter;
	}
	
	@Bean
	public PedidoService pedidoService() {
		return new PedidoServiceImpl();
	}
	
	@Bean
	public RmiServiceExporter pedidoRmiServiceExporter()
	{
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
		rmiServiceExporter.setServiceName("PedidoService");
		rmiServiceExporter.setServiceInterface(PedidoService.class);
		rmiServiceExporter.setService(pedidoService());
		rmiServiceExporter.setRegistryPort(portRegistry.intValue());
		rmiServiceExporter.setServicePort(portService.intValue());
		return rmiServiceExporter;
	}
	
	@Bean
	public UsuarioService usuarioService() {
		return new UsuarioServiceImpl();
	}
	
	@Bean
	public RmiServiceExporter usuarioRmiServiceExporter()
	{
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
		rmiServiceExporter.setServiceName("UsuarioService");
		rmiServiceExporter.setServiceInterface(UsuarioService.class);
		rmiServiceExporter.setService(usuarioService());
		rmiServiceExporter.setRegistryPort(portRegistry.intValue());
		rmiServiceExporter.setServicePort(portService.intValue());
		return rmiServiceExporter;
	}
}
